#include <iostream>

using namespace std;

struct Node
{
    int data;
    Node* next;

    Node()
    {
        data = 0;
        next = nullptr;
    }

    Node(int x, Node* p = nullptr)
    {
        data = x;
        next = p;
    }
};

Node* head = new Node();

bool empty(void)
{
    return head->next == nullptr;
}

int length(void)
{
    return head->data;
}

void insert_first(int x)
{
    Node* tmp = new Node(x, head->next);
    head->next = tmp;
    head->data++;
}

void insert_last(int x)
{
    Node* p = head;
    while (p->next)
        p = p->next;
    p->next = new Node(x);
    head->data++;
}

void insert(int k, int x)
{
    if (!(k >= 0 && k <= head->data))
    {
        printf("不能再位置%d处插入, 正确范围是 %d ~ %d\n", k, 0, head->data);
        exit(EXIT_FAILURE);
    }

    int t = 0;
    Node* p = head;

    while (t != k)
    {
        t++;
        p = p->next;
    }

    Node* tmp = new Node(x, p->next);
    p->next = tmp;

    head->data++;
}

int erase_first(void)
{
    if (empty())
    {
        printf("链表没有元素可以删除\n");
        exit(EXIT_FAILURE);
    }

    Node* tmp = head->next->next;
    int data = head->next->data;
    delete head->next;
    head->next = tmp;
    head->data--;
}

int erase_last(void)
{
    if (empty())
    {
        printf("链表没有元素可以删除\n");
        exit(EXIT_FAILURE);
    }
    Node* p = head;
    while (p->next->next)
        p = p->next;
    Node* tmp = p->next;
    int data = tmp->data;
    p->next = tmp->next;
    delete tmp;
    head->data--;

    return data;
}

int erase(int k)
{
    if (!(k >= 1 && k <= head->data))
    {
        printf("删除的位置%d不正确\n", k);
        exit(EXIT_FAILURE);
    }
    int t = 0;
    Node* p = head;
    while (t != k - 1)
    {
        p = p->next;
        t++;
    }
    Node* tmp = p->next;
    int data = tmp->data;
    p->next = tmp->next;
    delete tmp;
    head->data--;
    return data;
}

int query_first(void)
{
    if (!head->next)
    {
        printf("链表不存在数据\n");
        exit(EXIT_FAILURE);
    }
    return head->next->data;
}

int query_last(void)
{
    if (!head->next)
    {
        printf("链表不存在数据\n");
        exit(EXIT_FAILURE);
    }
    Node* p = head;
    while (p->next)
        p = p->next;
    return p->data;
}

int query(int k)
{
    if (!(k >= 1 && k <= head->data))
    {
        printf("访问的位置有问题!\n");
        exit(EXIT_FAILURE);
    }
    int t = 0;
    Node* p = head;
    while (t != k)
    {
        t++;
        p = p->next;
    }

    return p->data;
}

void display()
{
    Node* p = head;
    while (p->next)
    {
        p = p->next;
        printf("%d ", p->data);
    }
    printf("\n");
}

void destroy()
{
    Node* p = head, * tmp;
    while (p)
    {
        tmp = p;
        p = p->next;
        delete tmp;
    }
}

int main(void)
{
    int m, x;
    char s[20];

    scanf("%d", &m);

    while (m--)
    {
        scanf("%s", s);
        if (s[3] == 'h')
        {
            scanf("%d", &x);
            insert_last(x);
        }
        else if (s[3] == '\0')
        {
            erase_last();
        }
        else if (s[3] == 't')
        {
            if (empty())
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
        {
            printf("%d\n", query_last());
        }
    }
}